package io.xccit.aischool.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.xccit.aischool.entity.Teacher;
import io.xccit.aischool.service.ITeacherService;
import io.xccit.aischool.util.MD5;
import io.xccit.aischool.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author CH_ywx
 * @date 2023-06-06
 * @description
 */
@Api(tags = "教师接口")
@RestController
@RequestMapping("/sms/teacherController")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    @ApiOperation("分页获取所有教师")
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    public Result<Page<Teacher>> getTeacherByOpr(@ApiParam("页数") @PathVariable Integer pageNo,
                                                      @ApiParam("每页总条数") @PathVariable Integer pageSize,
                                                      @ApiParam("老师名字,精确查询字段") @RequestParam @Nullable String teacherName){
        Page<Teacher> teacherPage = new Page<>(pageNo,pageSize);
        Page<Teacher> page = teacherService.getTeacherByOpr(teacherPage,teacherName);
        return Result.ok(page);
    }

    @ApiOperation("添加/更新教师")
    @PostMapping("/saveOrUpdateTeacher")
    public Result<Boolean> saveOrUpdate(@ApiParam("教师请求体") @RequestBody Teacher teacher){
        //密码加密
        String password = teacher.getPassword();
        teacher.setPassword(MD5.encrypt(password));
        return Result.ok(teacherService.saveOrUpdate(teacher));
    }

    /**
     * 删除/批量删除教师
     * @param ids
     * @return
     */
    @ApiOperation("删除/批量删除教师")
    @DeleteMapping("deleteTeacher")
    public Result<Boolean> deleteTeacher(@ApiParam("删除教师的id集合") @RequestBody List<Integer> ids){
        return Result.ok(teacherService.removeByIds(ids));
    }
}
